package cn.wolfcode.luowowo.member.service.impl;

import cn.wolfcode.luowowo.cache.service.IUserInfoRedisService;
import cn.wolfcode.luowowo.common.exception.LogicException;
import cn.wolfcode.luowowo.common.util.AssertUtil;
import cn.wolfcode.luowowo.common.util.Consts;
import cn.wolfcode.luowowo.member.domain.UserInfo;
import cn.wolfcode.luowowo.member.mapper.UserInfoMapper;
import cn.wolfcode.luowowo.member.service.IUserInfoService;
import com.alibaba.dubbo.config.annotation.Reference;
import com.alibaba.dubbo.config.annotation.Service;
import org.springframework.beans.factory.annotation.Autowired;

import java.util.List;
import java.util.UUID;

@Service  //注意: 是dubbo里面的服务标签
//@Transactional  项目4再讲
public class UserInfoServiceImpl implements IUserInfoService {


    //trip-member-server  调用 trip-member-api 接口
    //trip-member-server  调用 trip-member-sever 接口

    @Autowired  //组件与组件间调用
    private UserInfoMapper userInfoMapper;

    @Reference
    private IUserInfoRedisService userInfoRedisService;


    @Override
    public UserInfo get(long id) {
        return userInfoMapper.selectByPrimaryKey(id);
    }

    @Override
    public boolean checkPhone(String phone) {
        return userInfoMapper.selectCountByPhone(phone) > 0;
    }

    @Override
    public void sendVerifyCode(String phone) {

        //创建手机验证码
        String code = UUID.randomUUID().toString().substring(0, 4);

        //缓存到redis中 TODO
        userInfoRedisService.setVerfiyCode(phone, code);


        //发送手机验证码
        StringBuilder sb = new StringBuilder(100);
        sb.append("你的验证码是:").append(code).append(", 请在").append(Consts.VERIFY_CODE_VAI_TIME).append("分钟内使用");

        //假装已经发送了
        System.out.println(sb.toString());

    }

    @Override
    public void userRegist(String phone, String nickname, String password, String rpassword, String verifyCode) {

        //必须校验参数是否为空
        AssertUtil.hasLength(phone,"手机号码不能为空");
        AssertUtil.hasLength(nickname,"昵称不能为空");
        AssertUtil.hasLength(password,"密码不能为空");
        AssertUtil.hasLength(rpassword,"确认密码不能为空");
        AssertUtil.hasLength(verifyCode,"短信验证码不能为空");

        //必须校验密码是否相等
        AssertUtil.isEquals(password, rpassword, "两次输入密码不一致");

        //必须校验手机号码是否存在
        if(userInfoMapper.selectCountByPhone(phone)> 0){
            throw new LogicException("手机号码已经被注册了");
        }

        //必须校验短信验证码是否正确
        String redisCode = userInfoRedisService.getVerfiyCode(phone);
        if(!verifyCode.equalsIgnoreCase(redisCode)){
            throw new LogicException("验证码失效了或者不一致");
        }

        //注册
        UserInfo userInfo = new UserInfo();
        userInfo.setNickname(nickname);
        userInfo.setPassword(password);   //假装加密了
        userInfo.setPhone(phone);

        userInfo.setLevel(1);  //用户级别
        userInfo.setState(userInfo.STATE_NORMAL);

        userInfo.setHeadImgUrl("/images/default.jpg");
        userInfoMapper.insert(userInfo);
    }

    @Override
    public String userLogin(String username, String password) {

        //检查账号或密码是否正确
        UserInfo userInfo = userInfoMapper.selectUserByUsernameAndPassword(username, password);

        //判断是否存在
        if(userInfo == null){
            throw new LogicException("账号或密码错误");
        }

        //存在:存在一个token
        String token = UUID.randomUUID().toString().replace("-", "");

        //token作为key 存储redis中
        userInfoRedisService.addToken(token, userInfo);

        return token;
    }

    @Override
    public List<UserInfo> list() {
        return userInfoMapper.selectAll();
    }
}

